/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$(function() {
   
    if (typeof(Storage) !== "undefined")
    {

        //signup1
        if (sessionStorage.fn) {
            if ($('input[name="firstname"]')[0]) {
                $('input[name="firstname"]').val(sessionStorage.fn);
            }
        }
        if (sessionStorage.ln) {
            if ($('input[name="lastname"]')[0]) {
                $('input[name="lastname"]').val(sessionStorage.ln);
            }
        }
        if (sessionStorage.email) {
            if ($('input[name="email"]')[0]) {
                $('input[name="email"]').val(sessionStorage.email);
            }
        }
        if (sessionStorage.pass) {
            if ($('input[name="password"]')[0]) {
                $('input[name="password"]').prop('type', 'password');
                $('input[name="password"]').val(sessionStorage.pass);
                $('input[name="password_2"]').prop('type', 'password');
                $('input[name="password_2"]').val(sessionStorage.pass);
            }
        }
        //signup2
        if (sessionStorage.fiance_fn) {
            if ($('input[name="fiance_firstname"]')[0]) {
                $('input[name="fiance_firstname"]').val(sessionStorage.fiance_fn);
            }
        }
        if (sessionStorage.fiance_ln) {
            if ($('input[name="fiance_lastname"]')[0]) {
                $('input[name="fiance_lastname"]').val(sessionStorage.fiance_ln);
            }
        }
        if (sessionStorage.fiance_date) {
            if ($('input[name="fiance_date"]')[0]) {
                $('input[name="fiance_date"]').val(sessionStorage.fiance_date);
            }
        }
        if (sessionStorage.fiance_condition) {
            if ($('input[name="fiance_condition"]')[0]) {
                if (sessionStorage.fiance_condition == 1) {
                    $('input[name="fiance_condition"]').attr('checked', 'checked');
                }
            }
        }


    }
    $(".next-page").click(function() {

        if (checkValidate('error-msg')) {
            if (typeof(Storage) !== "undefined")
            {
                // Yes! localStorage and sessionStorage support!
                // Some code.....
                sessionStorage.fn = $('input[name="firstname"]').val();
                sessionStorage.ln = $('input[name="lastname"]').val();
                sessionStorage.email = $('input[name="email"]').val();
                sessionStorage.pass = $('input[name="password"]').val();

                wd_redirect("signUp2.html");
            }
            else
            {
                print_alert("Sorry! web app storage session was not supported!");
                // Sorry! No web storage support..
            }

        }
    });
    $(".back_sign_one").click(function() {

        if (typeof(Storage) !== "undefined")
        {
            // Yes! localStorage and sessionStorage support!
            // Some code.....
            sessionStorage.fiance_fn = $('input[name="fiance_firstname"]').val();
            sessionStorage.fiance_ln = $('input[name="fiance_lastname"]').val();
            sessionStorage.fiance_date = $('input[name="fiance_date"]').val();
            if ($('input[name="fiance_condition"]').is(':checked')) {
                sessionStorage.fiance_condition = 1;
            } else {
                sessionStorage.fiance_condition = 0;
            }


            wd_redirect("signUp1.html");
        }
        else
        {
            print_alert("Sorry! web app storage session was not supported!");
            // Sorry! No web storage support..
        }


    });

    $(".btn-submit-login").click(function() {

        if (checkValidate_signup_2('error-msg')) {
            $(".btn-submit-login").attr("disabled", "disabled");
            $.post(localStorage.domain_app+"/webservice/createUser.php", {
                firstName: sessionStorage.fn,
                lastName: sessionStorage.ln,
                userEmail: sessionStorage.email,
                password: sessionStorage.pass,
                userRole: 2,
                fianceFirstName: $('input[name="fiance_firstname"]').val(),
                fianceLastName: $('input[name="fiance_lastname"]').val(),
                weddingDate: $('input[name="fiance_date"]').val()

            }, function(res) {
                if (res) {

                    var msgEr = '';
                    var num_milliseconds = 3000;
                    if (res == 2) {
                        msgEr = "This email is already registered but has not been confirmed.";

                    } else if (res == 5) {
                        msgEr = "Email is already registered";

                    }


                    if (msgEr === '') {

                        print_alert("An email with an activation link has been sent to your inbox. Please check your inbox and activate your account.");
                        wd_redirect("login.html");
                    } else {
                        $('#error-msg').addClass('active').children().html(msgEr);
                        hideError_2('error-msg', num_milliseconds);

                    }
                    return false;
                } else {
                    navigator.notification.alert("Your signup failed", function() {
                    });
                }
                $(".btn-submit-login").removeAttr("disabled");
            }, "json");
        }
    });

    $('input[type="text"]').focus(function() {
        $(this).css("color", "#9A7E72");
        if (this.value == this.defaultValue) {
            $(this).val("");

        } else if (this.value != this.defaultValue) {

            this.select();
        }

    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            this.value = (this.defaultValue ? this.defaultValue : '');
            
        }
    });
    $('input[type="email"]').focus(function() {
        $(this).css("color", "#9A7E72");
        if (this.value == this.defaultValue) {
            $(this).val("");

        } else if (this.value != this.defaultValue) {

            this.select();
        }

    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            this.value = (this.defaultValue ? this.defaultValue : '');
            
        }
    });
    $('input[type="date"]').focus(function() {
        $(this).css("color", "#9A7E72");
        if (this.value == this.defaultValue) {
            $(this).val("");

        } else if (this.value != this.defaultValue) {
            
            this.select();
        }

    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            this.value = (this.defaultValue ? this.defaultValue : '');
            
        }
    });
    $('.signup-password').focus(function() {
        $(this).css("color", "#9A7E72");
        if (this.value !== '' && this.value !== "Password" && this.value !== "Re-password") {
            
            this.select();
        } else {
            $(this).val('').prop('type', 'password');
        }
    })
            .blur(function() {
        if ($(this).val() === '') {
            $(this).val($(this).attr('rel')).prop('type', 'text');
           
        }
    });

});
function checkValidate_signup_2(id_append) {
    var check_char = /^[a-zA-Z\s]+$/;
    // error msg depends on the language
    var errormsg = new Array();
    errormsg[1] = 'The %s field is required.';
    errormsg[2] = 'Please input your %s';
    errormsg[3] = 'The %s field must contain only numbers.';
    errormsg[4] = 'This %s address is invalid.';
    errormsg[5] = '%s is too short. %s2 must be at least %number characters';
    errormsg[6] = 'Confirm password does not match password';
    errormsg[7] = 'The %s field must not exceed %number characters in length.';
    errormsg[8] = 'This %s is invalid.';
    //show msg
    var msgEr = '';
    var valueStr = '';
    var num_milliseconds = 3000;
    if ($('input[name="fiance_firstname"]').val() == '' || $('input[name="fiance_firstname"]').val() == 'Fiance’s First Name') {
        msgEr = errormsg[2].replace('%s', 'fiance’s first name');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="fiance_firstname"]'), num_milliseconds);
        return false;
    } else {
        valueStr = $('input[name="fiance_firstname"]').val();
        if (!check_char.test(valueStr)) {
            $('#' + id_append + '').addClass('active').children().html("Fiance’s first name cannot include special characters");
            hideError(id_append, $('input[name="fiance_firstname"]'), num_milliseconds);
            return false;
        } 

    }

    if ($('input[name="fiance_lastname"]').val() == '' || $('input[name="fiance_lastname"]').val() == 'Fiance’s Last Name') {
        msgEr = errormsg[2].replace('%s', 'fiance’s last name');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="fiance_lastname"]'), num_milliseconds);
        return false;
    }
    else {
        valueStr = $('input[name="fiance_lastname"]').val();
        if (!check_char.test(valueStr)) {
            $('#' + id_append + '').addClass('active').children().html("Fiance’s last name cannot include special characters");
            hideError(id_append, $('input[name="fiance_lastname"]'), num_milliseconds);
            return false;
        } 

    }
    if ($('input[name="fiance_date"]').val() == '' || $('input[name="fiance_date"]').val() == 'Wedding Date (mm/dd/yy)') {
        msgEr = errormsg[2].replace('%s', 'wedding date');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="fiance_date"]'), num_milliseconds);
        return false;
    }
    else {
        var rxDatePattern = /(19|20)\d\d[- \/.](0?[1-9]|1[012])[- \/.](0?[1-9]|[12][0-9]|3[01])/;
        var date = $('input[name="fiance_date"]').val();
        if (!date.match(rxDatePattern)) {
            msgEr = errormsg[8].replace('%s', 'wedding date');
            $('#' + id_append + '').addClass('active').children().html(msgEr);
            hideError(id_append, $('input[name="fiance_date"]'), num_milliseconds);
            return false;
        } 
    }
    if (!$('input[name="fiance_condition"]').is(':checked')) {
        msgEr = "In order to use our services, you must agree to the Terms and Conditions of this application.";
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="fiance_condition"]'), num_milliseconds);
        return false;
    }
    return true;
}
function checkValidate(id_append) {
    var emailRegex = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,6}$/i,
            numericRegex = /^[0-9-]+$/;
    var check_char = /^[a-zA-Z\s]+$/;
    // error msg depends on the language
    var errormsg = new Array();
    errormsg[1] = 'The %s field is required.';
    errormsg[2] = 'Please input your %s';
    errormsg[3] = 'The %s field must contain only numbers.';
    errormsg[4] = 'This %s address is invalid.';
    errormsg[5] = '%s is too short. %s2 must be at least %number characters';
    errormsg[6] = 'Confirm password does not match password';
    errormsg[7] = 'The %s field must not exceed %number characters in length.';
    errormsg[8] = 'This %s is invalid.';
    //show msg
    var msgEr = '';
    var valueStr = '';
    var num_milliseconds = 3000;
    //check
    if ($('input[name="firstname"]').val() == '' || $('input[name="firstname"]').val() == 'First Name') {
        msgEr = errormsg[2].replace('%s', 'first name');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="firstname"]'), num_milliseconds);
        return false;
    } else {
        valueStr = $('input[name="firstname"]').val();
        if (!check_char.test(valueStr)) {
            $('#' + id_append + '').addClass('active').children().html("First name cannot include special characters");
            hideError(id_append, $('input[name="firstname"]'), num_milliseconds);
            return false;
        }

    }

    if ($('input[name="lastname"]').val() == '' || $('input[name="lastname"]').val() == 'Last Name') {
        msgEr = errormsg[2].replace('%s', 'last name');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="lastname"]'), num_milliseconds);
        return false;
    }
    else {
        valueStr = $('input[name="lastname"]').val();
        if (!check_char.test(valueStr)) {
            $('#' + id_append + '').addClass('active').children().html("Last name cannot include special characters");
            hideError(id_append, $('input[name="lastname"]'), num_milliseconds);
            return false;
        } 

    }


    if ($('input[name="email"]').val() == '' || $('input[name="email"]').val() == 'E-mail') {
        msgEr = errormsg[2].replace('%s', 'email');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="email"]'), num_milliseconds);
        return false;
    } else if ($('input[name="email"]').val() != '') {
        valueStr = $('input[name="email"]').val();
        if (!emailRegex.test(valueStr)) {
            msgEr = errormsg[4].replace('%s', 'email');
            $('#' + id_append + '').addClass('active').children().html(msgEr);
            hideError(id_append, $('input[name="email"]'), num_milliseconds);
            return false;
        } 
    }


    if ($('input[name="password"]').val() == '' || $('input[name="password"]').val() == 'Password') {
        msgEr = errormsg[2].replace('%s', 'password');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="password"]'), num_milliseconds);
        return false;
    } else if ($('input[name="password"]').val() != '') {
        valueStr = $('input[name="password"]').val();
        var valueStr2 = $('input[name="password_2"]').val();
        if (!isLengthBetween(valueStr, 6)) {
            msgEr = errormsg[5].replace('%s', 'Password').replace('%s2', 'Password').replace('%number', '6');
            $('#' + id_append + '').addClass('active').children().html(msgEr);
            hideError(id_append, $('input[name="password"]'), num_milliseconds);
            return false;
        } else if (valueStr != valueStr2) {
            msgEr = errormsg[6];
            $('#' + id_append + '').addClass('active').children().html(msgEr);
            hideError(id_append, $('input[name="password_2"]'), num_milliseconds);
            return false;
        }
    } 

    return true;
}
function hideError(id_append, item, num_milliseconds) {
    item.css("color", "red");
    setTimeout(function() {
        $('#' + id_append + '').fadeOut('fast', function() {
//            $('#'+id_append+'').show();
        });
        $('#' + id_append + '').removeClass('active').children().html('');
    }, num_milliseconds);
}
function hideError_2(id_append, num_milliseconds) {
    setTimeout(function() {
        $('#' + id_append + '').fadeOut('fast', function() {
        });
        $('#' + id_append + '').removeClass('active').children().html('');
    }, num_milliseconds);
}
function isLengthBetween(str, min, max) {
    if (max) {
        return (str.length >= min) && (str.length <= max);
    } else {
        return (str.length >= min);
    }
}